﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using BankCharts.Silverlight.ViewModel.SubViewModel;
using GalaSoft.MvvmLight.Command;

namespace BankCharts.Silverlight.ViewModel.Models
{
    /// <summary>
    /// Contains 
    /// </summary>
    public class Category : ViewModelBase
    {
        private CategoryManagerVM _parent;

        public Category(CategoryManagerVM parent)
        {
            _parent = parent;
            PrepareCommand();
        }

        #region Name
        private string _Name = string.Empty;

        /// <summary>
        /// Gets the Name property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                if (_Name == value)
                {
                    return;
                }

                _Name = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("Name");
            }
        }
        #endregion

        #region Tokens
        private ObservableCollection<string> _Tokens = new ObservableCollection<string>();

        /// <summary>
        /// Gets the Tokens property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<string> Tokens
        {
            get
            {
                return _Tokens;
            }

            set
            {
                if (_Tokens == value)
                {
                    return;
                }

                _Tokens = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("Tokens");
            }
        }
        #endregion

        #region AffectedBankLines
        private ObservableCollection<BankLine> _AffectedBankLines = new ObservableCollection<BankLine>();

        /// <summary>
        /// Gets the AffectedBankLines property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<BankLine> AffectedBankLines
        {
            get
            {
                return _AffectedBankLines;
            }

            set
            {
                if (_AffectedBankLines == value)
                {
                    return;
                }

                _AffectedBankLines = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("AffectedBankLines");
                RaisePropertyChanged("ResultAmount");
            }
        }
        #endregion

        #region ResultAmount

        public float ResultAmount
        {
            get
            {
                float value = 0;
                foreach (var item in AffectedBankLines)
                {
                    value += item.Value;
                }

                return Math.Abs(value);
            }
        }
        #endregion

        #region Commands

        public RelayCommand RemoveCommand { get; set; }
        public RelayCommand<string> RemoveTokenCommand { get; set; }

        public void PrepareCommand()
        {
            RemoveCommand = new RelayCommand(Remove);
            RemoveTokenCommand = new RelayCommand<string>(RemoveToken);
        }

        public void RemoveToken(string s)
        {
            Tokens.Remove(s);
        }

        public void Remove()
        {
            _parent.RemoveCategory(this);
        }
        #endregion
    }
}
